﻿using System;
using System.Collections.Generic;
using System.Text;
using DGui;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using RobotGame.ScreenManagers;
using RobotGameData;

namespace RobotGame.ScreenManagers
{
    public class MessageBoxScreen : GameScreen
    {
        private Texture2D backgroundTexture;
        private Vector2 backgroundPosition;
        private Texture2D loadingBlackTexture;
        private Rectangle loadingBlackTextureDestination;

        public string[] textButtons;
        public Vector2[] PostionButtons;
        public Dictionary<string, Button> buttonEvent = new Dictionary<string, Button>();

        public string[] TextboxName;
        public Vector2[] PostionTextbox;
        public Dictionary<string, DTextBox> DTextbox = new Dictionary<string, DTextBox>();

        DGuiManager guiManager;

        private SpriteFont font;
        public MessageBoxScreen()
        {
            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.2);
            TransitionOffTime = TimeSpan.FromSeconds(0.2);
        }
        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            font = content.Load<SpriteFont>("Fonts\\menufont");

            backgroundTexture = content.Load<Texture2D>(@"Textures\MainMenus\MessageInfo");
            loadingBlackTexture =
               content.Load<Texture2D>(@"Textures\MainMenus\FadeScreen");
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            backgroundPosition = new Vector2(
                (viewport.Width - backgroundTexture.Width) / 2,
                (viewport.Height - backgroundTexture.Height) / 2);
            loadingBlackTextureDestination = new Rectangle(viewport.X, viewport.Y,
                viewport.Width, viewport.Height);
           
            guiManager = new DGuiManager(ScreenManager.Game, ScreenManager.SpriteBatch);
            // load button info
            textButtons = new string[] { "Leave" };
            PostionButtons = new Vector2[]  
            {
                new Vector2(200,200),
            };
            for (int index = 0; index < textButtons.Length; index++)
            {
                Button button = new Button("ButtonNormal", "ButtonSelects", true);
                button.LoadContent(content, "MainMenus");
                button.Position = PostionButtons[index];
                button.Bounds = new Rectangle((int)PostionButtons[index].X, (int)PostionButtons[index].Y, button.RegularTexture.Width + 50, button.RegularTexture.Height);
                buttonEvent.Add(textButtons[index], button);
            }
            buttonEvent["Leave"].Click += new EventHandler(MessageBoxScreen_Click);

            // load textbox
            TextboxName = new string[] { "txtText" };
            PostionTextbox = new Vector2[] 
            { 
                new Vector2(400, 400)
            };
            for (int index = 0; index < TextboxName.Length; index++)
            {
                DTextBox textBox = new DTextBox(guiManager, PostionTextbox[index].X, PostionTextbox[index].Y, 100, 24);
                textBox.Initialize();
                DTextbox.Add(TextboxName[index], textBox);
                guiManager.AddControl(textBox);
            }
           
        }

        void MessageBoxScreen_Click(object sender, EventArgs e)
        {
           this.ExitScreen();
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            for (int index = 0; index < textButtons.Length; index++)
            {
                buttonEvent[textButtons[index]].HandleInput(input.CurrentMousestate);
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime,otherScreenHasFocus,coveredByOtherScreen);
            //guiManager.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();
            spriteBatch.Draw(loadingBlackTexture, loadingBlackTextureDestination,
             Color.White);
            spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
           
            spriteBatch.End();

            DrawButtons(gameTime);

            //guiManager.Draw(gameTime);
        }

        public  void DrawButtons(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            for (int index = 0; index < textButtons.Length; index++)
            {
                buttonEvent[textButtons[index]].Draw(gameTime, spriteBatch);
            }
            spriteBatch.Begin();
            for (int index = 0; index < textButtons.Length; index++)
            {
                if (!String.IsNullOrEmpty(textButtons[index]))
                {
                    Vector2 size = font.MeasureString(textButtons[index]);
                    Vector2 textPosition = new Vector2(buttonEvent[textButtons[index]].Bounds.X +
                          (buttonEvent[textButtons[index]].RegularTexture.Width + 50) / 2 - size.X / 2,
                          buttonEvent[textButtons[index]].Bounds.Y + buttonEvent[textButtons[index]].RegularTexture.Height / 2 - size.Y / 2);
                    spriteBatch.DrawString(font, textButtons[index], textPosition, Color.Black);
                }
            }
            spriteBatch.End();

        }
    }
}
